<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Profile extends CI_Controller {
    public function __construct() {
        parent::__construct();
        
        $this->load->model('user_model');
    }
    
    public function show_profile_form() {
        $this->load->helper('util');
        if (!isset($_SESSION['userinfo'])) {
            $data['title'] = '您尚未登录';
            $data['message'] = '请登录后再尝试此项操作，谢谢。';
            $this->load->view('header');
            $this->load->view('navbar');
            $this->load->view('message', $data);
            $this->load->view('footer');
        }
        else {
            $data['info'] = $this->user_model->get_optional_info($_SESSION['userinfo']['user_id']);
            if ( isset($data['info']) )
                $data['info']['constel'] = value_to_constel($data['info']['constel']);
            $this->load->view('header');
            $this->load->view('navbar');
            $this->load->view('profile/profile_form', $data);
            $this->load->view('footer');
        }
    }
    
    public function update_profile() {
        $retJson = array();
        $this->load->helper('util');
        if (!isset($_SESSION['userinfo'])) {
            $retJson['code'] = -1;
            $retJson['error_message'] = "用户未登录";
        }
        else {
            if( $this->_check_profile_data($this->input->post())) {
                if( $this->input->post('year_of_birth') == NULL || checkbirthdate($this->input->post('year_of_birth')) ) {            
                    $this->user_model->update($_SESSION['userinfo']['user_id'], "nickname", $this->input->post('nickname'));
                    $this->user_model->update_optional_info($_SESSION['userinfo']['user_id'], $this->input->post());
                	$this->user_model->refresh_session();
                 	$retJson['code'] = 1;
            	}
				else {
            	    $retJson['code'] = -2;
            	    $retJson['error_message'] = "出生年份不合法";
            	}
							
            }
            else {
                $retJson['code'] = -3;
                $retJson['error_message'] = "存在字段超过限制";
            }
        }
        $this->output->set_content_type('application/json')
             ->set_output(json_encode($retJson));
    }
    
    public function show_profile_password() {
        if (!isset($_SESSION['userinfo'])) {
            $data['title'] = '您尚未登录';
            $data['message'] = '请登录后再尝试此项操作，谢谢。';
            $this->load->view('header');
            $this->load->view('navbar');
            $this->load->view('message', $data);
            $this->load->view('footer');
        }
        else {
            $this->load->view('header');
            $this->load->view('navbar');
            $this->load->view('profile/profile_password');
            $this->load->view('footer');
        }
    }
    
    public function update_password() {
        $retJson = array();
        if (!isset($_SESSION['userinfo'])) {
            $retJson['code'] = -1;
            $retJson['error_message'] = "用户未登录";
        }
        else {            
            $res = $this->user_model->login(array( 
                    "user_id" => $_SESSION['userinfo']['user_id'],
                    "password" => $this->input->post('oldpassword')
                    ));
            if ($res === FALSE) {
                $retJson['code'] = -2;
                $retJson['error_message'] = "原密码输入错误";
            }
            else {
                $this->user_model->update_password($_SESSION['userinfo']['user_id'],
                        $this->input->post('newpassword'),
                        $this->input->post('newsalt')
                        );
                $retJson['code'] = 1;
            }
        }
        $this->output->set_content_type('application/json')
        ->set_output(json_encode($retJson));
    }
    
    private function _check_profile_data($info) {
        if( $info['living_place'] != NULL && mb_strlen($info['living_place'],'UTF-8') > 50 ) {
            return false;
        }
        else if( $info['is_single'] != NULL && $info['is_single'] > 2 ) {
            return false;
        }
        else if( $info['constel'] != NULL && $info['constel'] > 12 ) {
            return false;
        }
		
		else if ($info['qq_show'] != NULL && ((!is_numeric($info['qq_show']))||(mb_strlen($info['qq_show'],'UTF-8') < 5))){
            return false; //qq ：5 or more numbers
        }
		 else if ($info['email_show'] != NULL &&!preg_match("/^[\w\-\.]+@[\w\-]+(\.\w+)+$/",$info['email_show'])) {
			return false;  //email ：regular expression
		} 
        else {
            return true;
        }
    }
}

?>
